package com.yt.demo;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class DiyRealm extends AuthenticatingRealm {
    //重点，重写此方法
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username=authenticationToken.getPrincipal().toString();
        String pwd =new String((char[]) authenticationToken.getCredentials());
        System.out.println("diy== username:"+username);
        System.out.println("diy== password:"+pwd );
        AuthenticationInfo info=null;
        if(username.equals("yangtao")){
            //加盐迭代3次后的密码
           String pwdinfo="ac2b1f6a1b75fa34038bd374674fd321";
            info=new SimpleAuthenticationInfo(
                    authenticationToken.getPrincipal()
                    ,pwdinfo, ByteSource.Util.bytes("salt"),
                    authenticationToken.getPrincipal().toString());
        }

        return info;
    }
}
